package com.renwu.edu.info.manager.dao;

import com.renwu.edu.info.manager.domain.Teacher;

public class TeacherDao {
    private static Teacher[] teachers = new Teacher[5];

    public static Teacher[] findAllTeacher() {
        return teachers;
    }

    public boolean AddTeacher(Teacher tea) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            if(teachers[i] == null){
                index = i;
                break;
            }
        }
        if(index == -1){
            return false;
        }else{
            teachers[index] = tea;
            return true;
        }
    }

    public void deleteTeacher(int id) {
        int index = getIndex(id);
        //对数组的索引位置使用null替换
            teachers[index] = null;
    }
    public int getIndex(int id){
        int index = -1;
        //遍历数组拿到每一个学生对象，对象不为null且该对象的ID值和需要删除的ID值相同，则记录该数组的索引位置
        for (int i = 0; i < teachers.length; i++) {
            if(teachers[i] != null && teachers[i].getId() == id){
                index = i;
                break;
            }
        }
        return index;
    }

    public void setTeacher(int id, Teacher teacher) {
        int index = getIndex(id);
        teachers[index] = teacher;
    }
}
